package practice2;

import java.util.Scanner;

public class P8641 {
    public static int n;
    public static int max = 0;
    public static int[] num;
    public static void f() {
        for (int i = 0; i < n; i++) {//从第i个卡片开始
            int[] temp = new int[n];//临时数组
            for (int k = 0; k < n; k++)
                temp[k] = num[k];
            int sum = 0;//每次数到卡片的和
            int count = 1;//从第一张开始数
            int start = i;//第i张卡片
            while (true) {
                boolean judge = false;
                for (int k = 0; k < n; k++)
                    if (temp[k] >= count) {//判断大于count的，否者就直接跳出
                        judge = true;
                        break;
                    }
                if (!judge)//如果不为真就直接跳出
                    break;
                int j = start % n;
                if (temp[j] == count) {
                    sum = sum + count;//每次数到卡片的和加起来
                    temp[j] = -1;//读到过了
                    count = 1;//重新从1开始读
                } else if (temp[j] != -1)
                    count++;
                start++;
            }
            max = Math.max(max, sum);//每次比出最大的值
        }
        System.out.println(max);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        num = new int[n];
        for (int i = 0; i < n; i++)
            num[i] = in.nextInt();
        f();
    }
}